package com.draw.cli.gui;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class Loading extends JFrame {
	private static final long serialVersionUID = -4371096181421358972L;
	
	private static Loading instance;
	private static int count = 0;
	
	public static void start () {
		if ( instance == null ) {
			instance = new Loading();
		}
		count++;
	}
	
	public static void stop () {
		count--;
		if ( instance != null && count <= 0 ) {
			instance.abort();
			instance = null;
		}
	}

	private JProgressBar progress;

	public Loading() {
		this.setLayout(new BorderLayout());
		this.setTitle("Chargement...");
		this.add(new JLabel("Chargement..."), BorderLayout.NORTH);
		this.progress = new JProgressBar();
		this.progress.setIndeterminate(true);
		this.progress.setVisible(true);
		this.add(this.progress, BorderLayout.CENTER);
		this.setLocationRelativeTo(null);
		this.setAlwaysOnTop(true);
		this.setResizable(false);
		this.setAutoRequestFocus(true);
		this.pack();
		this.setVisible(true);
	}

	public void abort() {
		this.progress.setIndeterminate(false);
		this.setVisible(false);
	}
}
